﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Business.Sales;

namespace ESOL.WinUI.Sales
{
    public partial class frmManageWorkingPlan : BaseForm.frmBaseMdiBusinessForm
    {
        #region Constructor
        public frmManageWorkingPlan()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            this.cmbPerson.SelectedIndexChanged -= new System.EventHandler(this.cmbPerson_SelectedIndexChanged);
            List<MstPersonEntity> listPerson = new List<MstPersonEntity>();
            MstPersonBs.GetPersonListBySupervisor(CommonParameter.CurrentUser.PersonId, listPerson);
            MstPersonEntity entity = new MstPersonEntity();
            listPerson.Insert(0, entity);

            cmbPerson.DataSource = listPerson;
            cmbPerson.DisplayMember = "NameCn";
            cmbPerson.ValueMember = "PersonId";
            this.cmbPerson.SelectedIndexChanged += new System.EventHandler(this.cmbPerson_SelectedIndexChanged);
        }

        private void BindDataGird()
        {
            DateTime monday = Convert.ToDateTime(dtpMonday.EditValue);
            if (dtpMonday.Text == "")
            {
                DateTime today = DateTime.Now;
                while (today.DayOfWeek != DayOfWeek.Monday)
                {
                    today = today.AddDays(-1);
                }
                monday = today;
            }
            gridView1.Columns[1].Caption = "星期一（" + monday.ToString("yyyy-MM-dd") + "）";
            gridView1.Columns[2].Caption = "星期二（" + monday.AddDays(1).ToString("yyyy-MM-dd") + "）";
            gridView1.Columns[3].Caption = "星期三（" + monday.AddDays(2).ToString("yyyy-MM-dd") + "）";
            gridView1.Columns[4].Caption = "星期四（" + monday.AddDays(3).ToString("yyyy-MM-dd") + "）";
            gridView1.Columns[5].Caption = "星期五（" + monday.AddDays(4).ToString("yyyy-MM-dd") + "）";
            gridView1.Columns[6].Caption = "星期六（" + monday.AddDays(5).ToString("yyyy-MM-dd") + "）";
            gridView1.Columns[7].Caption = "星期日（" + monday.AddDays(6).ToString("yyyy-MM-dd") + "）";

            if (cmbPerson.SelectedValue != null && cmbPerson.SelectedValue.ToString().Trim() != "")
            {
                string personId = cmbPerson.SelectedValue.ToString().Trim();
                
                DataTable dt = SalesWorkingPlanBs.GetDataForOverView(personId, monday);
                gvMain.DataSource = dt;
            }
            else
            {

                DataTable dt = SalesWorkingPlanBs.GetDataForOverView("", monday);
                dt.Rows.Clear();
                List<MstPersonEntity> listPerson = (List<MstPersonEntity>)cmbPerson.DataSource;
                
                if (listPerson != null && listPerson.Count > 0)
                {
                    foreach (MstPersonEntity person in listPerson)
                    {
                        if (person == null || string.IsNullOrEmpty(person.PersonId))
                        {
                            continue;
                        }
                        if (chkIsWork.Checked)
                        {
                            if (person.Status != Convert.ToString(Common.PersonStatus.PS01))
                            {
                                continue;
                            }
                            
                        }
                        else
                        {
                            if (person.Status == Convert.ToString(Common.PersonStatus.PS01))
                            {
                                continue;
                            }
                        }
                        DataTable dtTemp = SalesWorkingPlanBs.GetDataForOverView(person.PersonId, monday);
                        foreach (DataRow dr in dtTemp.Rows)
                        {
                            DataRow row = dt.NewRow();
                            foreach (DataColumn col in dtTemp.Columns)
                            {
                                row[col.ColumnName] = dr[col];
                            }
                            dt.Rows.Add(row);
                        }
                    }
                }
                dt.AcceptChanges();
                gvMain.DataSource = dt;
            }
        }

        private void ResetDateTimePicker()
        {
            if (dtpMonday.Text == "")
                return;
            this.dtpMonday.EditValueChanged -= new System.EventHandler(this.dtpMonday_EditValueChanged);
            while (Convert.ToDateTime(dtpMonday.EditValue).DayOfWeek != DayOfWeek.Monday)
            {
                dtpMonday.EditValue = Convert.ToDateTime(dtpMonday.EditValue).AddDays(-1);
            }
            this.dtpMonday.EditValueChanged += new System.EventHandler(this.dtpMonday_EditValueChanged);
        }
        #endregion

        private void frmManageWorkingPlan_Load(object sender, EventArgs e)
        {
            dtpMonday.EditValue = DateTime.Now;
            ResetDateTimePicker();
            BindComboBox();
            BindDataGird();
            chkIsWork.EditValue = true;
        }

        private void dtpMonday_EditValueChanged(object sender, EventArgs e)
        {
            ResetDateTimePicker();
            BindDataGird();
        }

        private void cmbPerson_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindDataGird();
        }
        protected override void Click_View()
        {
            if (mSelectedRow == null)
            {
                MessageBox.Show("请选择一条人员信息", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmManagePersonalWorkPlan frm = new frmManagePersonalWorkPlan();

            MstPersonEntity person = MstPersonBs.GetMstPersonByID(mSelectedRow["PersonId"].ToString());
            if (person != null)
            {
                frm = new frmManagePersonalWorkPlan(person, Convert.ToDateTime(dtpMonday.EditValue));
            }
            frm.MdiParent = this.MdiParent;
            frm.Show();
            
        }

        DataRow mSelectedRow;
        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            mSelectedRow = gridView1.GetDataRow(e.RowHandle);
        }

        protected override void Click_Query()
        {
            BindDataGird();
        }

        private void chkIsWork_CheckedChanged(object sender, EventArgs e)
        {
            BindDataGird();
        }

    }
}